package views
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.net.URLRequest;
	
	import mx.core.UIComponent;
	
	public class NabaztagView extends UIComponent
	{
		private var rightEar: NabEarView;
		private var leftEar: NabEarView;
		private var loader: Loader;
		private var rightLed: Led;
		private var leftLed: Led;
		private var middleLed: Led;
		private var noseLed: Led;
		
		public function NabaztagView()
		{
			super();
		}
		
		override protected function createChildren(): void {
			super.createChildren();
			
			createBody();
			
		}
		
		private function createBody(): void {
			var urlReq: URLRequest =  new URLRequest("assets/img/nabaztagBody.png");
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handle_loadComplete);
			loader.load(urlReq);
		}
		
		private function createEars(): void {
			rightEar = new NabEarView();
			leftEar = new NabEarView();
			addChild(leftEar);
			rightEar.x = 90;
			rightEar.y = 20;
			leftEar.x = 156;
			leftEar.y = 20;
		}
		
		private function createLeds(): void {
			var blur: BlurFilter = new BlurFilter(10, 10);
			rightLed = configureLed();
			middleLed = configureLed();
			leftLed = configureLed();
			noseLed = configureLed();
			leftLed.x = 94;
			leftLed.y = 182;
			middleLed.x = 132;
			middleLed.y = 185;
			rightLed.x = 170;
			rightLed.y = 182;
			noseLed.x = 132;
			noseLed.y = 110;
			//noseLed.
			//noseLed.graphics.endFill();
		}
		
		private function configureLed(): Led {
			var aLed: Led = new Led();
			aLed.filters = [new BlurFilter(10, 10)];
			aLed.alpha = 0.7;
			addChild(aLed);
			return aLed;
		}
		
		private function handle_loadComplete(event: Event): void {
			var bodyImage: Bitmap = Bitmap(loader.content);
			var body: Sprite = new Sprite();
			this.addEventListener(MouseEvent.MOUSE_MOVE, handle_mouseMove);
			body.addChild(bodyImage);
			createEars();
			addChild(body);
			createLeds();
		}
		
		private function handle_mouseMove(event: MouseEvent): void {
			stage.nativeWindow.startMove();
		}
		
		public function set leftLedColor(aString: String): void {
			leftLed.transform.colorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
		}

	}
}